package cn.stylefeng.guns.modular.basic.service.impl;

import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.ArchiveBox;
import cn.stylefeng.guns.modular.basic.mapper.ArchiveBoxMapper;
import cn.stylefeng.guns.modular.basic.model.params.ArchiveBoxParam;
import cn.stylefeng.guns.modular.basic.model.result.ArchiveBoxResult;
import cn.stylefeng.guns.modular.basic.service.ArchiveBoxService;
import cn.stylefeng.guns.modular.basic.service.CommonService;
import cn.stylefeng.guns.modular.manager.entity.ArchiveInfo;
import cn.stylefeng.guns.modular.manager.model.params.ArchiveInfoParam;
import cn.stylefeng.guns.modular.manager.service.ArchiveInfoService;
import cn.stylefeng.guns.sys.core.constant.state.YesNo;
import cn.stylefeng.guns.utils.ServiceAssert;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 档案盒信息表 服务实现类
 * </p>
 *
 * @author blesslp
 * @since 2019-11-17
 */
@Service
public class ArchiveBoxServiceImpl extends ServiceImpl<ArchiveBoxMapper, ArchiveBox> implements ArchiveBoxService {

    @Autowired
    private ArchiveInfoService archiveInfoService;
    @Autowired
    private CommonService commonService;

    @Override
    public boolean hasArchiveInfo(String key) {
        return archiveInfoService.lambdaQuery()
                .eq(ArchiveInfo::getDahid, key)
                .count() > 0;
    }

    @Override
    public void add(ArchiveBoxParam param) {
        ArchiveBox entity = getEntity(param);
        entity.setSl(YesNo.NO.getCode());
        this.save(entity);
    }

    @Override
    @Transactional
    public void delete(ArchiveBoxParam param) {
        //删除档案盒
        final Serializable key = getKey(param);
        boolean isSuccess = this.removeById(key);
        String TIPS = "删除失败";
        ServiceAssert.assetTrue(isSuccess,TIPS);

        final List<ArchiveInfo> list = this.archiveInfoService.lambdaQuery()
                .select(ArchiveInfo::getId)
                .eq(ArchiveInfo::getDahid, key)
                .list();

        for (ArchiveInfo archiveInfo : list) {
            ArchiveInfoParam archiveInfoParam = new ArchiveInfoParam();
            archiveInfoParam.setId(archiveInfo.getId());
            this.archiveInfoService.delete(archiveInfoParam);
        }
    }

    @Override
    public void update(ArchiveBoxParam param) {
        ArchiveBox oldEntity = getOldEntity(param);
        ArchiveBox newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ArchiveBoxResult findBySpec(ArchiveBoxParam param) {
        return this.baseMapper.findBySpec(param);
    }

    @Override
    public List<ArchiveBoxResult> findListBySpec(ArchiveBoxParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ArchiveBoxParam param) {
        Page pageContext = getPageContext();

        if (ToolUtil.isNotEmpty(param.getSsbmid())) {
            final List<String> childDepts = this.commonService.getChildDepts(Long.parseLong(param.getSsbmid()));
            childDepts.add(0, param.getSsbmid());
            if (ToolUtil.isNotEmpty(childDepts)) {
                param.setSsbmidArr(childDepts);
            }
        }

        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ArchiveBoxParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ArchiveBox getOldEntity(ArchiveBoxParam param) {
        return this.getById(getKey(param));
    }

    private ArchiveBox getEntity(ArchiveBoxParam param) {
        ArchiveBox entity = new ArchiveBox();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public List<ZTreeNode> tree(ArchiveBoxParam param) {
        return this.baseMapper.tree(param);
    }

    @Override
    public String getMaxCode(String ssdasid, String ssdagid, String ssdagcid) {
        return this.baseMapper.getMaxCode(ssdasid,ssdagid,ssdagcid);
    }
}
